﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CarbonExchange.Bll;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CarbonExchange.Tests
{
    [TestClass]
    public class AuditTests : BaseTest
    {
        [TestMethod]
        public void Audit_GetById_WithIncorrectId_ReturnsNull()
        {
            AuditFactory factory = new AuditFactory(_Context);
            Assert.IsNull(factory.GetById(int.MaxValue));
        }

        [TestMethod]
        public void Audit_GetById_WithCorrectId_ReturnsNotNull()
        {
            AuditFactory factory = new AuditFactory(_Context);
            Assert.IsNotNull(factory.GetById(_TestAudits[0].Id));
        }

        [TestMethod]
        public void Audit_GetById_WithCorrectId_PopulatesCorrectly()
        {
            AuditFactory factory = new AuditFactory(_Context);
            Audit tmp = factory.GetById(_TestAudits[0].Id);
            Assert.IsNotNull(tmp);
            Assert.AreEqual(_TestAudits[0].Id, tmp.Id);
            Assert.AreEqual(_TestAudits[0].AuditKey, tmp.AuditKey);
            Assert.AreEqual(_TestAudits[0].Created.ToLocalTime(), tmp.Created);
            Assert.AreEqual(_TestAudits[0].EndDate.ToLocalTime(), tmp.EndDate);
            Assert.AreEqual(_TestAudits[0].StartDate.ToLocalTime(), tmp.StartDate);
            Assert.AreEqual(_TestAudits[0].UserId, tmp.UserId);
            Assert.AreEqual(_TestAudits[0].Country, tmp.Country);
            Assert.AreEqual(_TestAudits[0].State, tmp.State);
            Assert.AreEqual(_TestAudits[0].PeopleInHousehold, tmp.PeopleInHousehold);
        }

        [TestMethod]
        public void Audit_GetAllByUserId_WithIncorrectId_IsEmpty()
        {
            AuditFactory factory = new AuditFactory(_Context);
            var tmp = factory.GetAllByUserId(int.MaxValue);
            Assert.IsNotNull(tmp);
            Assert.AreEqual(0, tmp.Count());
        }

        [TestMethod]
        public void Audit_GetAllByUserId_WithCorrectId_IsNotEmpty()
        {
            AuditFactory factory = new AuditFactory(_Context);
            var tmp = factory.GetAllByUserId(_TestUsers[0].Id);
            Assert.IsNotNull(tmp);
            Assert.AreEqual(2, tmp.Count());
        }

        [TestMethod]
        public void Audit_GetAllByUserId_WithCorrectId_PopulatesCorrectly()
        {
            AuditFactory factory = new AuditFactory(_Context);
            var tmpList = factory.GetAllByUserId(_TestUsers[0].Id).ToList();
            Assert.IsNotNull(tmpList);
            Assert.AreEqual(2, tmpList.Count());
            var tmp = tmpList[1];
            Assert.AreEqual(_TestAudits[0].Id, tmp.Id);
            Assert.AreEqual(_TestAudits[0].AuditKey, tmp.AuditKey);
            Assert.AreEqual(_TestAudits[0].Created.ToLocalTime(), tmp.Created);
            Assert.AreEqual(_TestAudits[0].EndDate.ToLocalTime(), tmp.EndDate);
            Assert.AreEqual(_TestAudits[0].StartDate.ToLocalTime(), tmp.StartDate);
            Assert.AreEqual(_TestAudits[0].UserId, tmp.UserId);
            Assert.AreEqual(_TestAudits[0].Country, tmp.Country);
            Assert.AreEqual(_TestAudits[0].State, tmp.State);
            Assert.AreEqual(_TestAudits[0].PeopleInHousehold, tmp.PeopleInHousehold);
        }

        [TestMethod]
        public void Audit_GetCurrentForUserId_WithCorrectId_PopulatesCorrectly()
        {
            AuditFactory factory = new AuditFactory(_Context);
            var tmp = factory.GetCurrentForUserId(_TestUsers[0].Id);
            Assert.AreEqual(_TestAudits[_TestAudits.Count() - 1].Id, tmp.Id);
            Assert.AreEqual(_TestAudits[_TestAudits.Count() - 1].AuditKey, tmp.AuditKey);
            Assert.AreEqual(_TestAudits[_TestAudits.Count() - 1].Created.ToLocalTime(), tmp.Created);
            Assert.AreEqual(_TestAudits[_TestAudits.Count() - 1].EndDate.ToLocalTime(), tmp.EndDate);
            Assert.AreEqual(_TestAudits[_TestAudits.Count() - 1].StartDate.ToLocalTime(), tmp.StartDate);
            Assert.AreEqual(_TestAudits[_TestAudits.Count() - 1].UserId, tmp.UserId);
            Assert.AreEqual(_TestAudits[_TestAudits.Count() - 1].Country, tmp.Country);
            Assert.AreEqual(_TestAudits[_TestAudits.Count() - 1].State, tmp.State);
            Assert.AreEqual(_TestAudits[_TestAudits.Count() - 1].PeopleInHousehold, tmp.PeopleInHousehold);
        }

        [TestMethod]
        public void Audit_DeleteOne()
        {
            AuditFactory factory = new AuditFactory(_Context);
            var tmp = factory.GetById(_TestAudits[0].Id);
            Assert.IsNotNull(tmp);
            factory.Delete(tmp);
            tmp = factory.GetById(_TestAudits[0].Id);
            Assert.IsNull(tmp);
        }

        [TestMethod]
        public void Audit_DeleteMany()
        {
            AuditFactory factory = new AuditFactory(_Context);
            var tmp = factory.GetAllByUserId(_TestUsers[0].Id);
            Assert.IsNotNull(tmp);
            Assert.AreEqual(2, tmp.Count());
            factory.Delete(tmp);
            tmp = factory.GetAllByUserId(_TestUsers[0].Id);
            Assert.IsNotNull(tmp);
            Assert.AreEqual(0, tmp.Count());
        }


        [TestMethod]
        public void Audit_UpdateOne()
        {
            AuditFactory factory = new AuditFactory(_Context);
            var tmp = factory.GetById(_TestAudits[0].Id);
            Assert.IsNotNull(tmp);

            tmp.PeopleInHousehold = 25;
            tmp.State = "State";
            tmp.CountryId = _TestCountries.First().Id;
            factory.Save(tmp);

            Assert.AreEqual(25, tmp.PeopleInHousehold);
            Assert.AreEqual("State", tmp.State);
            Assert.AreEqual("Country", tmp.Country);

            var tmp2 = factory.GetById(_TestAudits[0].Id);
            Assert.IsNotNull(tmp2);
            Assert.AreEqual(tmp.Id, tmp2.Id);
            Assert.AreEqual(tmp.AuditKey, tmp2.AuditKey);
            Assert.AreEqual(tmp.EndDate, tmp2.EndDate);
            Assert.AreEqual(tmp.StartDate, tmp2.StartDate);
            Assert.AreEqual(tmp.UserId, tmp2.UserId);
            Assert.AreEqual(tmp.State, tmp2.State);
            Assert.AreEqual(tmp.Country, tmp2.Country);
            Assert.AreEqual(tmp.PeopleInHousehold, tmp2.PeopleInHousehold);
        }

        [TestMethod]
        public void Audit_UpdateMany()
        {
            AuditFactory factory = new AuditFactory(_Context);
            var tmpList = factory.GetAllByUserId(_TestUsers[0].Id).ToList();
            Assert.IsNotNull(tmpList);
            Assert.AreEqual(2, tmpList.Count());
            foreach (Audit tmp in tmpList)
            {
                var i = tmpList.IndexOf(tmp);
                tmp.State = string.Format("Updated State {0}", i);
                tmp.CountryId = _TestCountries.First().Id;
                tmp.PeopleInHousehold = 100 - i;
            }
            factory.Save(tmpList);
           
            var tmpList2 = factory.GetAllByUserId(_TestUsers[0].Id).ToList();
            Assert.IsNotNull(tmpList2);
            Assert.AreEqual(tmpList.Count(), tmpList2.Count());
            foreach (Audit tmp in tmpList)
            {
                var tmp2 = tmpList2[tmpList.IndexOf(tmp)];
                Assert.AreEqual(tmp.Id, tmp2.Id);
                Assert.AreEqual(tmp.AuditKey, tmp2.AuditKey);
                Assert.AreEqual(tmp.EndDate, tmp2.EndDate);
                Assert.AreEqual(tmp.StartDate, tmp2.StartDate);
                Assert.AreEqual(tmp.UserId, tmp2.UserId);
                Assert.AreEqual(tmp.PeopleInHousehold, tmp2.PeopleInHousehold);
                Assert.AreEqual(tmp.Country, tmp2.Country);
                Assert.AreEqual(tmp.State, tmp2.State);
            }
        }

        [TestMethod]
        public void Audit_InsertOne()
        {
            AuditFactory factory = new AuditFactory(_Context);
            DateTime endDate = DateTime.Parse("12/5/2007");
            Audit tmp = new Audit(_TestUsers[1].Id, endDate);
            tmp.PeopleInHousehold = 5;
            tmp.CountryId = _TestCountries.First().Id;
            tmp.State = "Utah";
            Assert.AreEqual(endDate, tmp.EndDate);
            Assert.AreEqual(endDate.AddYears(-1), tmp.StartDate);
            Assert.AreEqual(_TestUsers[1].Id, tmp.UserId);

            DateTime expected = DateTime.Now;
            factory.Save(tmp);

            Assert.AreEqual(endDate, tmp.EndDate);
            Assert.AreEqual(endDate.AddYears(-1), tmp.StartDate);
            Assert.AreEqual(_TestUsers[1].Id, tmp.UserId);
            Assert.IsTrue(tmp.Created - expected < TimeSpan.FromSeconds(1));
            Assert.IsFalse(string.IsNullOrEmpty(tmp.AuditKey));
            Assert.AreEqual("United States", tmp.Country);
            Assert.AreEqual("Utah", tmp.State);
            Assert.AreEqual(5, tmp.PeopleInHousehold);

            AmeeProfileFactory profileFactory = new AmeeProfileFactory();
            var profileItem = profileFactory.GetById(tmp.AuditKey);
            Assert.IsNotNull(profileItem);

            Assert.AreEqual(tmp.PeopleInHousehold, profileItem.PeopleInHoushold);
            Assert.AreEqual(tmp.StartDate, profileItem.StartDate);
            Assert.AreEqual(tmp.PeopleInHousehold, profileItem.PeopleInHoushold);
            Assert.AreEqual(tmp.PeopleInHousehold, profileItem.PeopleInHoushold);
            Assert.AreEqual(tmp.PeopleInHousehold, profileItem.PeopleInHoushold);

            var tmp2 = factory.GetById(tmp.Id);

            Assert.AreEqual(tmp.EndDate, tmp2.EndDate);
            Assert.AreEqual(tmp.StartDate, tmp2.StartDate);
            Assert.AreEqual(tmp.UserId, tmp2.UserId);
            Assert.AreEqual(tmp.Created, tmp2.Created);
            Assert.AreEqual(tmp.AuditKey, tmp2.AuditKey);
            Assert.AreEqual(tmp.Country, tmp2.Country);
            Assert.AreEqual(tmp.State, tmp2.State);
            Assert.AreEqual(tmp.PeopleInHousehold, tmp2.PeopleInHousehold);
        }
    }
}
